package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.utils.ConvertUtils;
import net.pinnoocle.playcommon.utils.HelpUtils;
import net.pinnoocle.playdao.dao.InformDao;
import net.pinnoocle.playdao.entity.Inform;
import net.pinnoocle.playdao.entity.admin.InformDetailVo;
import net.pinnoocle.playdao.entity.admin.InformListPageVo;
import net.pinnoocle.playdao.entity.admin.InformListSearchParams;
import net.pinnoocle.playdao.entity.admin.SaveInformParams;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 通知(Inform)表服务实现类
 *
 * @author ye
 * @since 2021-11-02 11:23:37
 */
@Service("informService")
@Slf4j
public class InformServiceImpl extends ServiceImpl<InformDao, Inform> {

    @Autowired
    ShopServiceImpl shopService;
    @Autowired
    UserServiceImpl userService;

    /**
     * 通知列表---分页
     * @param params
     * @return
     */
    public PageModel<InformListPageVo> informList(InformListSearchParams params){
        Map<String,Object> pageMap = new HashMap<>();
        pageMap.put("page",String.valueOf(params.getPage()));
        pageMap.put("pageSize",String.valueOf(params.getPageSize()));

        if(params.getShopId() == null){
            params.setShopId(userService.getGlobalShopId());
        }
        Page<InformListPageVo> list = this.baseMapper.informList(
                new PageQuery<Inform>(pageMap).getPage(),params);
        List<InformListPageVo> records = list.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageModel<>(list, new ArrayList<>());
        }
        for (InformListPageVo item : records){
            String timeStr = HelpUtils.easyReadTime(item.getCreatedTime());
            item.setFormatTimeStr(timeStr);
        }
        return new PageModel<>(list, records);
    }

    /**
     * 公告详情
     * @param informId
     * @return
     */
    public InformDetailVo informDetail(String informId){
        Inform inform = this.getById(informId);
        InformDetailVo informDetailVo = ConvertUtils.sourceToTarget(inform, InformDetailVo.class);
        //易读时间
        String timeStr = HelpUtils.easyReadTime(informDetailVo.getCreatedTime());
        informDetailVo.setFormatTimeStr(timeStr);
        return informDetailVo;
    }

    /**
     * 添加公告
     * @param params
     * @return
     */
    public boolean saveInform(SaveInformParams params){
        int type = params.getType();
        Inform inform = ConvertUtils.sourceToTarget(params, Inform.class);
        if(type == 1){
            String shopId = shopService.getShopId();
            inform.setShopId(shopId);
        }else{
            inform.setType(0);
        }
        inform.setCreatedTime(new Date());
        try {
            return this.save(inform);
        } catch (Exception e) {
            log.info("添加公告失败："+e.getMessage());
        }
        return false;
    }

    /**
     * 编辑公告
     * @param params
     * @return
     */
    public boolean updateInform(SaveInformParams params){
        int type = params.getType();
        Inform inform = ConvertUtils.sourceToTarget(params, Inform.class);
        if(type == 1){
            String shopId = shopService.getShopId();
            inform.setShopId(shopId);
        }else{
            inform.setType(0);
        }
        try {
            return this.updateById(inform);
        } catch (Exception e) {
            log.info("编辑公告失败："+e.getMessage());
        }
        return false;
    }

    /**
     * 删除公告
     * @param informId
     * @return
     */
    public boolean deleteInform(String informId){
        try {
            return this.removeById(informId);
        } catch (Exception e) {
            log.info("公告删除失败："+e.getMessage());
            return false;
        }
    }

}